# -*- coding: utf-8 -*-

from PalmWebsite import app
from flask.ext.restful import Api
from flask.ext.sqlalchemy import SQLAlchemy
import json
from flask import make_response


class MyApi(Api):

    def output_json(self, data, code, headers=None):
        resp = make_response(json.dumps(data, indent=4, separators=(',', ': '), ensure_ascii=False),
                            code)
        resp.headers.extend(headers or {})
        return resp

    def make_response(self, data, *args, **kwargs):
        for mediatype in self.mediatypes() + [self.default_mediatype]:
            if mediatype in self.representations:
                resp = self.representations[mediatype](data, *args, **kwargs)
                resp.headers['Content-Type'] = mediatype + "; charset=utf-8"
                return resp

    def __init__(self, *args, **kwargs):
        Api.__init__(self, *args, **kwargs)
        self.representations = {
            'application/json': self.output_json
        }
restful_api = MyApi(app)
db = SQLAlchemy(app)
